package com.ncmmall.application.msm;

import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 阿里云短信
 *
 * @author linxs
 */
@Slf4j
public class AliYunSmsService {

    private static final IAcsClient client = new DefaultAcsClient(DefaultProfile.getProfile("cn-shenzhen",
//            PropertiesHelps.getValue("aliyun_accessKey_id"), PropertiesHelps.getValue("aliyun_access_secret")));
            "LTAIjq2s5C390tpR", "8JpniDvA9dGITxz0RweyuJAabhyGTz"));

    /**
     * 短信发送
     *
     * @param mobile       手机号码
     * @param signName     短信签名
     * @param templateCode 短信模板编码
     * @param templatePram 短信模板参数，可为null
     */
    public static void send(String mobile, String signName, String templateCode, Map<String, String> templatePram) {

        CommonRequest request = new CommonRequest();

        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);

        if (templatePram != null && templatePram.size() > 0) {
            request.putQueryParameter("TemplateParam", JSONUtil.toJsonStr(templatePram));
        }

        try {
            client.getCommonResponse(request);
        } catch (Exception e) {
            log.error("[发送阿里云短信验证码异常]", e);
        }
    }

}
